// pages/map/map.js
import { convertBD09ToGCJ02, convertGCJ02TOBD09 } from '../../utils/util.js'
import QQMapWX from '../../utils/qqmap-wx-js.min';
const qqmapsdk = new QQMapWX({
  key: 'ZOUBZ-AJGKU-KZRVO-BFSFF-W4BYV-TWB2J' // 必填
});
Page({
  data: {
    mapKey: 'ZOUBZ-AJGKU-KZRVO-BFSFF-W4BYV-TWB2J',
    latitude: '',
    longitude: '',
    markerAll: [],
    markers: [],
    showItem: false,
    oneMarker: {}
  },

  onLoad: function (options) {
    var _this = this;
    // 获取用户地理位置
    wx.getLocation({
      success: function (res) {
        const gcPoint = convertBD09ToGCJ02(res.longitude, res.latitude);
        // const gcPoint = convertGCJ02TOBD09(res.longitude, res.latitude);
        _this.setData({
          latitude: gcPoint.lat,
          longitude: gcPoint.lng
        }, () => {
          _this.nearbySearch();
        });
      }
    });
  },
  // 获取周围影院
  nearbySearch() {
    var _this = this;
    const { latitude, longitude } = _this.data;
    // 调用接口
    qqmapsdk.search({
      keyword: '英语学习中心',
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        var mks = []
        for (var i = 0; i < res.data.length; i++) {
          mks.push({
            // title: res.data[i].title,
            id: res.data[i].id,
            latitude: res.data[i].location.lat,
            longitude: res.data[i].location.lng,
            category: res.data[i].category,
            address: res.data[i].address,
            iconPath: "../../static/image/coordinate.png",
            width: 40,
            height: 40
          });
        }
        _this.setData({
          markers: mks,
          markerAll: res.data
        })
      }
    });
  },
  markertap(e) {
    const { markerAll } = this.data;
    const { markerId } = e;
    markerAll.map(item => {
      if (markerId == item.id) {
        this.setData({
          oneMarker: item,
          showItem: true
        });
      }
    });
  },
  closeShow() {
    this.setData({
      showItem: false
    });
  }
})